package com.gz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gz.entity.Book;
import com.gz.entity.BookCollection;
import com.gz.entity.MyPageData;
import com.gz.entity.Vo.BookVo;
import com.gz.mapper.BookMapper;
import com.gz.service.BookCollectionService;
import com.gz.mapper.BookCollectionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 龙龙与贝儿
* @description 针对表【t_book_collection】的数据库操作Service实现
* @createDate 2025-05-23 21:16:27
*/
@Service
public class BookCollectionServiceImpl extends ServiceImpl<BookCollectionMapper, BookCollection>
    implements BookCollectionService{

    @Autowired
    private BookCollectionMapper bookCollectionMapper;
    @Autowired
    private BookMapper bookMapper;

    @Override
//    @CachePut(cacheNames="bookCollection",key="#userId")
    public void addCollection(Long userId, Long bookId) {
        BookCollection bookCollection = new BookCollection(userId,bookId);
        bookCollectionMapper.insert(bookCollection);
    }

    @Override
//    @CacheEvict(cacheNames="bookCollection",key="#userId")
    public void deleteCollection(Long userId, Long bookId) {
        LambdaQueryWrapper<BookCollection> wrapper = Wrappers.lambdaQuery(BookCollection.class).eq(BookCollection::getUserId, userId).eq(BookCollection::getBookId, bookId);
        bookCollectionMapper.delete(wrapper);
    }

    @Override
    public Boolean checkIsStorage(Long userId, Long bookId) {
        LambdaQueryWrapper<BookCollection> wrapper = Wrappers.lambdaQuery(BookCollection.class).eq(BookCollection::getUserId, userId).eq(BookCollection::getBookId, bookId);
        BookCollection bookCollection = bookCollectionMapper.selectOne(wrapper);
        if(bookCollection==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<BookCollection> selectCollectionStorageByUserId(Long userId) {
        LambdaQueryWrapper<BookCollection> wrapper = Wrappers.lambdaQuery(BookCollection.class).eq(BookCollection::getUserId, userId);
        List<BookCollection> bookCollections = bookCollectionMapper.selectList(wrapper);
        return bookCollections;
    }

    @Override
//    @Cacheable(cacheNames="bookCollection",key="#userId")
    public MyPageData getPageByBookIds(Long userId, int pageNum, int pageSize) {
        List<BookCollection> bookCollections = this.selectCollectionStorageByUserId(userId);
        List<Long> bookIds = new ArrayList<>();
        for (BookCollection bookCollection : bookCollections) {
            bookIds.add(bookCollection.getBookId());
        }
        System.out.println(bookIds);
        //通过id查询
        Page<Book> page = new Page<>(pageNum, pageSize);
        if (bookIds == null || bookIds.isEmpty()) {
            return null; // 返回空分页对象
        }

        //分页查询
        // 使用in条件查询多个ID
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery()
                .in(Book::getId, bookIds);
        Page<Book> page1 = bookMapper.selectPage(page, wrapper);

        List<Book> books = page1.getRecords();
        List<BookVo> bookTypeInfoVOS = new ArrayList<>();
        books.forEach(book -> {
            BookVo bookTypeInfoVO = new BookVo();
            BeanUtils.copyProperties(book, bookTypeInfoVO);
            bookTypeInfoVOS.add(bookTypeInfoVO);
        });
        MyPageData data = new  MyPageData(
                page1.getTotal(),
                bookTypeInfoVOS);
        return data;
    }
}




